package com.lxs.legou.pay.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @Auther: Mcb
 * @Date: 2022/1/30 23:20
 * @Description:
 */
@Configuration
public class MqConfig {

    @Autowired
    private Environment environment;

    /**
     * 定义路由器
     * @return
     */
    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder.directExchange(environment.getProperty("mq.pay.exchange.order"))
                .durable(true)
                .build();
    }

    /**
     * 定义队列
     * @return
     */
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable(environment.getProperty("mq.pay.queue.order")).build();
    }

    /**
     * 将路由和队列进行绑定
     * @param exchange
     * @param queue
     * @return
     */
    @Bean
    public Binding orderBinding(@Qualifier("orderExchange") Exchange exchange,
                                @Qualifier("orderQueue")Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(environment.getProperty("mq.pay.routing.key")).noargs();
    }
}
